/**
 * Created by johwa on 2014/10/8.
 */
var mongodb = require('./db');
var MongoClient = require('mongodb').MongoClient, format = require('util').format;
var dbHost = 'mongodb://127.0.0.1:27017/testblog';

function User(user){
    this.name = user.name;
    this.password = user.password;
}

//var User = function(){};

/*exports.User = function User(user){
    this.name = user.name;
    this.password = user.password;
};*/

module.exports = User;



User.prototype.save = function save(callback){
    //存入mongo文档
    var user = {
        name: this.name,
        password:this.password
    }

    MongoClient.connect(dbHost,function(err,db){
        if(err){
            return callback(err);
        }

        //var dbCollection = db.collection('user_table');
        db.collection('user_table',function(err,collection){
            if(err){
                db.close();
                return callback(err);
            }
            //建立索引
            collection.ensureIndex('name',{unique:true},function(err,user){});
            //插入数据
            collection.insert(user,{safe:true},function(err,userCollect){
                db.close();
                callback(err,userCollect)
            });

        });


    })


   /* mongodb.open(function(err,db){
        if(err){
            return callback(err);
        }
        //读取user集合
        db.collection('users',function(err,collection){
            if(err){
                mongodb.close();
                return callback(err);
            }

            //为name属性添加索引
            collection.ensureIndex('name',{unique:true});
            //console.log(user);
            //写入user文档
            collection.insert(user,{safe:true},function(err,user){
                //mongodb.close();

                callback(err,user);
            });

        });
    });*/
}

User.get = function get(username,callback){

    MongoClient.connect(dbHost,function(err,db){
        if(err) return callback(err);

        db.collection('user_table',function(err,collection){
            if(err){
                db.close();
                return callback(err);
            }

            collection.findOne({name:username},function(err,userCollect){
                //console.log(userCollect);
                db.close();
                if(userCollect){
                    var user = new User(userCollect);
                    callback(err,user);
                }else{
                    callback(err,null);
                }
            });

        });
    })


    /*mongodb.open(function(err,db){
        if(err){
            return callback(err);
        }

        db.collection('user_table',function(err,collection){
            if(err){
                mongodb.close();
                return callback(err);
            }

            collection.findOne({name:username},function(err,doc){
                db.close();
                if(doc){
                    var user = new User(doc);
                    callback(err,user);
                }else {
                    callback(err,null);
                }
            });

        });
    });*/
};